package com.example.daibanshiwu.Service.Impl;

import com.example.daibanshiwu.Service.GroupService;
import com.example.daibanshiwu.mapper.GroupMapper;
import com.example.daibanshiwu.pojo.Group;
import com.example.daibanshiwu.pojo.SimpleGroupDTO;
import com.example.daibanshiwu.pojo.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    public GroupMapper groupMapper;

    @Override
    public List<Group> getGroupMembers(){
        return groupMapper.findGroup();
    }
//    public Integer deleteGroup(String groupId){
//
//        return groupMapper.deleteGroup(groupId);
//    }
public void deleteGroups(List<String> groupIds) {
    // 增加一个校验，防止传入空列表导致SQL错误
    if (groupIds == null || groupIds.isEmpty()) {
        // 可以直接返回或抛出异常
        return;
    }
//    groupMapper.deleteMessagesByGroupIds(groupIds);
    groupMapper.deleteGroupMembersByGroupIds(groupIds);
    groupMapper.deleteGroups(groupIds);

}
    public List<TUser> findAllUser(String name){
        return groupMapper.findAllUser(name);
    }
    public List<TUser> findAllUser(){
        return groupMapper.findAllUser();
    }
    public List<TUser> findUsersByGroupIds(List<String> groupIdList){
        return groupMapper.getUsersByGroupId(groupIdList);
    }

    public  int updateGroupStatus(String groupId, String newStatus){
        return groupMapper.updateStatus(groupId,newStatus);

    }

    public int addGroup(Group group) {
        group.setCreateTime(LocalDate.now());
        groupMapper.insertGroup(group);
        groupMapper.insertGroupMember(group.getMemberList(),group.getGroupId());
        return 1;
    }
    // 我们暂时创建一个新的方法来做测试，不影响你原来的方法
    public List<SimpleGroupDTO> getSimpleGroups() {
        // 1. 从数据库查询出完整的 Group 列表，这一步我们知道是成功的
        List<Group> fullGroupList = groupMapper.findGroup();

        // 2. 将完整的 Group 对象，转换为最简单的 DTO 对象
        return fullGroupList.stream()
                .map(group -> {
                    SimpleGroupDTO dto = new SimpleGroupDTO();
                    dto.setGroupId(group.getGroupId());
                    dto.setGroupName(group.getGroupName());
                    return dto;
                })
                .collect(Collectors.toList());
    }
}
